<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2023-09-24 15:09:21
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 加料单
 */

namespace App\Http\Services\Finance;


use App\Http\Services\Service;
use App\Repositories\FinanceInvoiceRepository;
use App\Repositories\FinanceInvoiceDetailsRepository;
use Illuminate\Support\Facades\DB;

/**
 * @property FinanceInvoiceRepository $repository
 * @property FinanceInvoiceDetailsRepository $details
 */
class InvoiceService extends Service
    {
    public $state = 1;
    public function boot(FinanceInvoiceRepository $repository, FinanceInvoiceDetailsRepository $details)
        {
        $this->repository = $repository;
        $this->details    = $details;
        }

    public function list()
        {
        $data = $this->repository->condition()->getGrid();
        return $data;
        }
    public function invoice_status()
        {
        $invoice_status_id = request('invoice_status_id');
        $this->getMain()->filter(function ($v) use ($invoice_status_id)
            {
            $v->invoice_status_id = $invoice_status_id;
            $v->details->filter(function ($vv) use ($invoice_status_id)
                {
                $vv->invoice_status_id = $invoice_status_id;
                $vv->save();
                });
            $v->save();
            });
        }
    public function details()
        {
        $data = $this->details->condition()->getGrid();
        return $data;
        }
    protected function save()
        {
        $gdata = request();
        if (is_numeric($gdata['id']) && $gdata['id'] > 0) {
            $ddata = $this->repository->whereId($gdata['id'])->first();
            if ($ddata->status_id != 0) {
                fail("单据:[{$ddata->code}]已{$ddata->status->name}！不能编辑！");
                }
            }
        $gdata         = $gdata->toArray();
        $gdata         = $this->repository->autosave($gdata);
        $gdata->amount = 0;
        $gdata->details->filter(function ($v) use ($gdata)
            {
            if (!is_numeric($v->tax_rate)) {
                fail("税额必须是数字！");
                }
            if (!is_numeric($v->amount)) {
                fail("金额必须是数字！");
                }
            $gdata->amount += $v->amount;
            $v->code         = $gdata->code;
            $v->company_id   = $gdata->company_id;
            $v->trade_id     = $gdata->trade_id;
            $v->billtrade    = $gdata->billtrade;
            $v->billdate     = $gdata->billdate;
            $v->remark       = $gdata->remark;
            $v->state_id     = $gdata->state_id;
            $v->tax_amount   = $gdata->tax_rate / 100 * $v->amount;//税额
            $v->notax_amount = $v->amount - $v->tax_amount;//不含税金额
            $v->save();
            });
        return $gdata->save();
        }
    private function getMain()
        {
        $ids = $this->details->whereIn("id", $this->getRequestIds())->pluck("main_id");

        return $this->repository->whereIn("id", $ids)->get();
        }
    protected function audit()
        {
        return $this->getMain()->filter(function ($gdata)
            {
            if ($gdata->status_id != 0) {
                fail("单据:[{$gdata->code}]已{$gdata->status->name}！不能审核！");
                }
            $gdata->details->filter(fn($v) => auditSave($v));
            auditSave($gdata);
            $gdata->finance();
            return $gdata;
            });
        }
    protected function noaudit()
        {
        return $this->getMain()->filter(function ($gdata)
            {
            if ($gdata->status_id == 0) {
                fail("单据:[{$gdata->code}]已{$gdata->status->name}！不能反审！");
                }
            $gdata->nofinance();
            $gdata->details->filter(fn($v) => auditSave($v, false));
            auditSave($gdata, false);
            return $gdata;
            });
        }
    protected function delete()
        {
        return $this->getMain()->filter(function ($gdata)
            {
            if ($gdata->status_id != 0) {
                fail("单据已{$gdata->status->name}！禁止删除");
                }
            $gdata->details->filter(fn($v) => $v->delete());
            return $gdata->delete();
            });
        }
    }